package com.guiji.file.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.guiji.common.utils.PageUtils;
import com.guiji.common.core.page.PagePlus;
import com.guiji.common.core.page.TableDataInfo;
import com.guiji.file.bo.CmsBucketAddBo;
import com.guiji.file.bo.CmsBucketEditBo;
import com.guiji.file.bo.CmsBucketQueryBo;
import com.guiji.file.domain.CmsBucket;
import com.guiji.file.mapper.CmsBucketMapper;
import com.guiji.file.service.ICmsBucketService;
import com.guiji.file.vo.CmsBucketVo;
import org.springframework.stereotype.Service;
import com.guiji.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;


import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2021-07-26
 */
@Service
public class CmsBucketServiceImpl extends ServicePlusImpl<CmsBucketMapper, CmsBucket> implements ICmsBucketService {

	@Override
	public CmsBucketVo queryById(Long bucketId){
		return getVoById(bucketId, CmsBucketVo.class);
	}

	@Override
	public TableDataInfo<CmsBucketVo> queryPageList(CmsBucketQueryBo bo) {
		PagePlus<CmsBucket, CmsBucketVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), CmsBucketVo.class);
		return PageUtils.buildDataInfo(result);
	}

	@Override
	public List<CmsBucketVo> queryList(CmsBucketQueryBo bo) {
		return listVo(buildQueryWrapper(bo), CmsBucketVo.class);
	}

	private LambdaQueryWrapper<CmsBucket> buildQueryWrapper(CmsBucketQueryBo bo) {
		Map<String, Object> params = bo.getParams();
		LambdaQueryWrapper<CmsBucket> lqw = Wrappers.lambdaQuery();
		lqw.eq(bo.getParentId() != null, CmsBucket::getParentId, bo.getParentId());
		lqw.like(StrUtil.isNotBlank(bo.getName()), CmsBucket::getName, bo.getName());
		return lqw;
	}

	@Override
	public Boolean insertByAddBo(CmsBucketAddBo bo) {
		CmsBucket add = BeanUtil.toBean(bo, CmsBucket.class);
		validEntityBeforeSave(add);
		return save(add);
	}

	@Override
	public Boolean updateByEditBo(CmsBucketEditBo bo) {
		CmsBucket update = BeanUtil.toBean(bo, CmsBucket.class);
		validEntityBeforeSave(update);
		return updateById(update);
	}

	/**
	 * 保存前的数据校验
	 *
	 * @param entity 实体类数据
	 */
	private void validEntityBeforeSave(CmsBucket entity){
		//TODO 做一些数据校验,如唯一约束
	}

	@Override
	public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
		if(isValid){
			//TODO 做一些业务上的校验,判断是否需要校验
		}
		return removeByIds(ids);
	}
}
